package top.codedance.iotp.tools.terminal.ui;

import top.codedance.iotp.common.util.AsymmetricEncryption;
import top.codedance.iotp.common.util.SystemUtil;
import top.codedance.iotp.tools.owner.ToolCallOwner;
import top.codedance.iotp.tools.terminal.TerminalApplication;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.io.File;
import java.net.URL;

/**
 *
 * @author xuedianfu
 */
public class UserVerificationCertificateManager extends javax.swing.JDialog {
    public ShellFrame sf;
    private final WaitLoading waitLoading = new WaitLoading(this);
    private final String PUBLIC_KEY_FILENAME = "publicKey.txt";
    private final String PRIVATE_KEY_FILENAME = "privateKey.txt";
    private static File lastFile = null;
    private ToolCallOwner owner = null;
    private int type = -1;

    /**
     * Creates new form UserVerificationCertificateManager
     */
    public UserVerificationCertificateManager(ShellFrame sf) {
        super(sf, true);
        this.sf = sf;
        initComponents();

        URL url = Thread.currentThread().getContextClassLoader().getResource("logo.gif");
        ImageIcon icon = new ImageIcon(url);
        setIconImage(icon.getImage());
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(this.sf);
        setTitle("代码飞舞采控系统-国密SM2密钥及密码生成器");

        jb_get1.setEnabled(false);
        jb_get2.setEnabled(false);
        jb_set1.setEnabled(false);
        jb_set2.setEnabled(false);
    }

    public UserVerificationCertificateManager(ShellFrame sf, ToolCallOwner owner, int type) {
        this(sf);
        this.owner = owner;
        this.type = type;

        URL url = Thread.currentThread().getContextClassLoader().getResource("logo.gif");
        ImageIcon icon = new ImageIcon(url);
        setIconImage(icon.getImage());
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        setTitle("代码飞舞采控系统-国密SM2密钥及密码生成器");

        jb_get1.setEnabled(true);
        jb_get2.setEnabled(true);
        jb_set1.setEnabled(true);
        jb_set2.setEnabled(true);
    }

    private void waiting(){
        waitLoading.open();
    }

    private void over(){
        if(waitLoading != null) {
            waitLoading.close();
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jp_top = new javax.swing.JPanel();
        jb_genKey = new javax.swing.JButton();
        jb_saveKey = new javax.swing.JButton();
        jb_cleanAll = new javax.swing.JButton();
        jb_clearPassword = new javax.swing.JButton();
        jp_left = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jta_publickey = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jta_privatekey = new javax.swing.JTextArea();
        jb_selectPrivateKey = new javax.swing.JButton();
        jb_selectPublicKey = new javax.swing.JButton();
        jp_right = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jta_plaintext = new javax.swing.JTextArea();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jta_ciphertext = new javax.swing.JTextArea();
        jb_genPassword = new javax.swing.JButton();
        jb_decode = new javax.swing.JButton();
        jb_cleanPlaintext = new javax.swing.JButton();
        jb_cleanCiphertext = new javax.swing.JButton();
        jb_get1 = new javax.swing.JButton();
        jb_set1 = new javax.swing.JButton();
        jb_get2 = new javax.swing.JButton();
        jb_set2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jp_top.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jb_genKey.setText("生成密钥");
        jb_genKey.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_genKeyActionPerformed(evt);
            }
        });

        jb_saveKey.setText("保存密钥到硬盘...");
        jb_saveKey.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_saveKeyActionPerformed(evt);
            }
        });

        jb_cleanAll.setText("清空所有");
        jb_cleanAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_cleanAllActionPerformed(evt);
            }
        });

        jb_clearPassword.setText("清空密码");
        jb_clearPassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_clearPasswordActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jp_topLayout = new javax.swing.GroupLayout(jp_top);
        jp_top.setLayout(jp_topLayout);
        jp_topLayout.setHorizontalGroup(
            jp_topLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jp_topLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jb_genKey)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jb_saveKey)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jb_clearPassword)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jb_cleanAll)
                .addContainerGap())
        );
        jp_topLayout.setVerticalGroup(
            jp_topLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jp_topLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jp_topLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jb_genKey)
                    .addComponent(jb_saveKey)
                    .addComponent(jb_cleanAll)
                    .addComponent(jb_clearPassword))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jp_left.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setText("公钥：");

        jta_publickey.setColumns(20);
        jta_publickey.setRows(5);
        jScrollPane1.setViewportView(jta_publickey);

        jLabel2.setText("私钥：");

        jta_privatekey.setColumns(20);
        jta_privatekey.setRows(5);
        jScrollPane2.setViewportView(jta_privatekey);

        jb_selectPrivateKey.setText("导入私钥...");
        jb_selectPrivateKey.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_selectPrivateKeyActionPerformed(evt);
            }
        });

        jb_selectPublicKey.setText("导入公钥...");
        jb_selectPublicKey.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_selectPublicKeyActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jp_leftLayout = new javax.swing.GroupLayout(jp_left);
        jp_left.setLayout(jp_leftLayout);
        jp_leftLayout.setHorizontalGroup(
            jp_leftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jp_leftLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jp_leftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 468, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jp_leftLayout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jb_selectPrivateKey))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jp_leftLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jb_selectPublicKey)))
                .addContainerGap())
        );
        jp_leftLayout.setVerticalGroup(
            jp_leftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jp_leftLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jp_leftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jb_selectPublicKey))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 235, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jp_leftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jb_selectPrivateKey))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
                .addContainerGap())
        );

        jp_right.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel3.setText("明文密码：");

        jta_plaintext.setColumns(20);
        jta_plaintext.setRows(5);
        jScrollPane3.setViewportView(jta_plaintext);

        jLabel4.setText("密文密码：");

        jta_ciphertext.setColumns(20);
        jta_ciphertext.setRows(5);
        jScrollPane4.setViewportView(jta_ciphertext);

        jb_genPassword.setText("生成密码");
        jb_genPassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_genPasswordActionPerformed(evt);
            }
        });

        jb_decode.setText("解密密码");
        jb_decode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_decodeActionPerformed(evt);
            }
        });

        jb_cleanPlaintext.setText("清空明文密码");
        jb_cleanPlaintext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_cleanPlaintextActionPerformed(evt);
            }
        });

        jb_cleanCiphertext.setText("清空密文密码");
        jb_cleanCiphertext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_cleanCiphertextActionPerformed(evt);
            }
        });

        jb_get1.setText("获取");
        jb_get1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_get1ActionPerformed(evt);
            }
        });

        jb_set1.setText("保存");
        jb_set1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_set1ActionPerformed(evt);
            }
        });

        jb_get2.setText("获取");
        jb_get2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_get2ActionPerformed(evt);
            }
        });

        jb_set2.setText("保存");
        jb_set2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_set2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jp_rightLayout = new javax.swing.GroupLayout(jp_right);
        jp_right.setLayout(jp_rightLayout);
        jp_rightLayout.setHorizontalGroup(
            jp_rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jp_rightLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jp_rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 458, Short.MAX_VALUE)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jp_rightLayout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jb_set1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jb_get1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jb_cleanPlaintext)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jb_decode))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jp_rightLayout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jb_set2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jb_get2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jb_cleanCiphertext)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jb_genPassword)))
                .addContainerGap())
        );
        jp_rightLayout.setVerticalGroup(
            jp_rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jp_rightLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jp_rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jb_decode)
                    .addComponent(jb_cleanPlaintext)
                    .addComponent(jb_get1)
                    .addComponent(jb_set1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jp_rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jb_genPassword)
                    .addComponent(jb_cleanCiphertext)
                    .addComponent(jb_get2)
                    .addComponent(jb_set2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane4)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jp_left, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jp_right, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jp_top, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jp_top, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jp_left, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jp_right, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jb_genKeyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_genKeyActionPerformed
        waiting();
        TerminalApplication.pool.submit(() -> {
            try {
                AsymmetricEncryption.KeyStringPair keyStringPair = AsymmetricEncryption.genKeyPair();
                jta_publickey.setText(keyStringPair.getPublicKey());
                jta_privatekey.setText(keyStringPair.getPrivateKey());
            }catch (Exception e){
                JOptionPane.showMessageDialog(rootPane, "生成密钥失败。");
            }finally {
                over();
            }
        });
    }//GEN-LAST:event_jb_genKeyActionPerformed

    private void jb_saveKeyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_saveKeyActionPerformed
        if(jta_publickey.getText().trim().isEmpty() || jta_privatekey.getText().trim().isEmpty()){
            JOptionPane.showMessageDialog(this.getRootPane(), "请选生成密钥，再保存");
            return;
        }
        JFileChooser jfc = new JFileChooser();
        if(lastFile != null) {
            jfc.setCurrentDirectory(lastFile);
        }
        jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int i = jfc.showOpenDialog(this);
        if(i == JFileChooser.APPROVE_OPTION) {
            File selectedFile = jfc.getSelectedFile();
            if (selectedFile.exists() && selectedFile.isDirectory()) {
                String publicKey = selectedFile.getAbsolutePath() + File.separatorChar + PUBLIC_KEY_FILENAME;
                String privateKey = selectedFile.getAbsolutePath() + File.separatorChar + PRIVATE_KEY_FILENAME;
                SystemUtil.writeFile(publicKey, jta_publickey.getText());
                SystemUtil.writeFile(privateKey, jta_privatekey.getText());
                lastFile = selectedFile;
                JOptionPane.showMessageDialog(this.getRootPane(), "导出成功,文件是所选目录下：公钥文件：" + publicKey + ", 私钥文件：" + privateKey);
            } else {
                JOptionPane.showMessageDialog(this.getRootPane(), "请选取一个文件夹");
            }
        }
    }//GEN-LAST:event_jb_saveKeyActionPerformed

    private void jb_genPasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_genPasswordActionPerformed
        if(jta_plaintext.getText().trim().isEmpty()){
            JOptionPane.showMessageDialog(this.getRootPane(), "请填入明文密码再进行加密操作。");
            return;
        }
        waiting();
        TerminalApplication.pool.submit(() -> {
            try {
                jta_ciphertext.setText(AsymmetricEncryption.encrypt(jta_plaintext.getText(), jta_publickey.getText()));
            }catch (Exception e){
                JOptionPane.showMessageDialog(this.getRootPane(), "加密失败!");
            }finally {
                over();
            }
        });
    }//GEN-LAST:event_jb_genPasswordActionPerformed

    private void jb_decodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_decodeActionPerformed
        if(jta_ciphertext.getText().trim().isEmpty()){
            JOptionPane.showMessageDialog(this.getRootPane(), "请填入密文密码再进行解密操作。");
            return;
        }
        waiting();
        TerminalApplication.pool.submit(() -> {
            try {
                jta_plaintext.setText(AsymmetricEncryption.decrypt(jta_ciphertext.getText(), jta_privatekey.getText()));
            }catch (Exception e){
                JOptionPane.showMessageDialog(this.getRootPane(), "解密失败!");
            }finally {
                over();
            }
        });
    }//GEN-LAST:event_jb_decodeActionPerformed

    private void jb_selectPrivateKeyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_selectPrivateKeyActionPerformed
        JFileChooser jfc = new JFileChooser();
        if(lastFile != null) {
            jfc.setCurrentDirectory(lastFile);
        }
        jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        jfc.setFileFilter(new FileFilter() {
            @Override
            public boolean accept(File f) {
                if (f.isDirectory())
                    return true;
                return f.getName().endsWith(".txt");
            }
            @Override
            public String getDescription(){
                return ".txt";
            }
        });
        int i = jfc.showOpenDialog(this);
        if(i == JFileChooser.APPROVE_OPTION){
            File selectedFile = jfc.getSelectedFile();
            if(selectedFile.exists() && selectedFile.isFile()) {
                jta_privatekey.setText(SystemUtil.readFile(selectedFile.getAbsolutePath()).trim());
                lastFile = selectedFile.getParentFile();
            }else{
                JOptionPane.showMessageDialog(this.getRootPane(), "请选取一个私钥文本文件");
            }
        }
    }//GEN-LAST:event_jb_selectPrivateKeyActionPerformed

    private void jb_selectPublicKeyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_selectPublicKeyActionPerformed
        JFileChooser jfc = new JFileChooser();
        if(lastFile != null) {
            jfc.setCurrentDirectory(lastFile);
        }
        jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        jfc.setFileFilter(new FileFilter() {
            @Override
            public boolean accept(File f) {
                if (f.isDirectory())
                    return true;
                return f.getName().endsWith(".txt");
            }
            @Override
            public String getDescription(){
                return ".txt";
            }
        });
        int i = jfc.showOpenDialog(this);
        if(i == JFileChooser.APPROVE_OPTION){
            File selectedFile = jfc.getSelectedFile();
            if(selectedFile.exists() && selectedFile.isFile()) {
                jta_publickey.setText(SystemUtil.readFile(selectedFile.getAbsolutePath()).trim());
                lastFile = selectedFile.getParentFile();
            }else{
                JOptionPane.showMessageDialog(this.getRootPane(), "请选取一个公钥文本文件");
            }
        }
    }//GEN-LAST:event_jb_selectPublicKeyActionPerformed

    private void jb_cleanAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_cleanAllActionPerformed
        jta_publickey.setText("");
        jta_privatekey.setText("");
        jta_plaintext.setText("");
        jta_ciphertext.setText("");
    }//GEN-LAST:event_jb_cleanAllActionPerformed

    private void jb_clearPasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_clearPasswordActionPerformed
        jta_plaintext.setText("");
        jta_ciphertext.setText("");
    }//GEN-LAST:event_jb_clearPasswordActionPerformed

    private void jb_cleanPlaintextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_cleanPlaintextActionPerformed
        jta_plaintext.setText("");
    }//GEN-LAST:event_jb_cleanPlaintextActionPerformed

    private void jb_cleanCiphertextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_cleanCiphertextActionPerformed
        jta_ciphertext.setText("");
    }//GEN-LAST:event_jb_cleanCiphertextActionPerformed

    private void jb_get1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_get1ActionPerformed
        if(owner != null) {
            jta_plaintext.setText(owner.getValue(type));
        }
    }//GEN-LAST:event_jb_get1ActionPerformed

    private void jb_get2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_get2ActionPerformed
        if(owner != null) {
            jta_ciphertext.setText(owner.getValue(type));
        }
    }//GEN-LAST:event_jb_get2ActionPerformed

    private void jb_set2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_set2ActionPerformed
        if(owner != null) {
            owner.setValue(jta_ciphertext.getText(), type);
            this.setVisible(false);
        }
    }//GEN-LAST:event_jb_set2ActionPerformed

    private void jb_set1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_set1ActionPerformed
        if(owner != null) {
            owner.setValue(jta_plaintext.getText(), type);
            this.setVisible(false);
        }
    }//GEN-LAST:event_jb_set1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JButton jb_cleanAll;
    private javax.swing.JButton jb_cleanCiphertext;
    private javax.swing.JButton jb_cleanPlaintext;
    private javax.swing.JButton jb_clearPassword;
    private javax.swing.JButton jb_decode;
    private javax.swing.JButton jb_genKey;
    private javax.swing.JButton jb_genPassword;
    private javax.swing.JButton jb_get1;
    private javax.swing.JButton jb_get2;
    private javax.swing.JButton jb_saveKey;
    private javax.swing.JButton jb_selectPrivateKey;
    private javax.swing.JButton jb_selectPublicKey;
    private javax.swing.JButton jb_set1;
    private javax.swing.JButton jb_set2;
    private javax.swing.JPanel jp_left;
    private javax.swing.JPanel jp_right;
    private javax.swing.JPanel jp_top;
    private javax.swing.JTextArea jta_ciphertext;
    private javax.swing.JTextArea jta_plaintext;
    private javax.swing.JTextArea jta_privatekey;
    private javax.swing.JTextArea jta_publickey;
    // End of variables declaration//GEN-END:variables
}
